package com.qna.web;

import java.io.File;
import java.security.Principal;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qna.UserDetails;
import com.qna.pojo.Question;

@Controller
public class AnswerController {

	@RequestMapping(value = "/answer")
	public String index(Model model, HttpServletRequest request, Principal principal) {
		Question userForm = null;
		String loggedInUser = principal.getName(); //get logged in username
		if(request.getParameter("timestamp") != null){
			String desktop = System.getProperty ("user.home") + "/Desktop/yo/";
			File questionFile = new File(desktop+"/"+request.getParameter("timestamp")+".xml");
			String answerresult = "Answer is incorrect";
			userForm = new Question();
			try {

				JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);
				Unmarshaller u = jaxbContext.createUnmarshaller();
				userForm = (Question)u.unmarshal(questionFile);
				if(userForm.getIsTrueFalse().toString().equalsIgnoreCase("true")){
					if(userForm.getIsTrue().toString().equalsIgnoreCase(request.getParameter("answer"))){
						answerresult = "CORRECT !!";
					}
					if(userForm.getIsFalse().toString().equalsIgnoreCase("true")
							&& request.getParameter("answer").equalsIgnoreCase("false")){
						answerresult = "CORRECT !!";
					}
				}
				if(userForm.getIsDropdown().toString().equalsIgnoreCase("true")){
					if(userForm.getAnswer().toString().equalsIgnoreCase(request.getParameter("answer"))){
						answerresult = "CORRECT !!";
					}

				}
				String userDetailsFolder = "userdetails";
				String userDetailsFilePath = desktop +"/"+userDetailsFolder+"/"+ loggedInUser +".xml";
				UserDetails userDetails = new UserDetails();
				File userDetailFile = new File(userDetailsFilePath);
				File[] files = new File(desktop +"/"+userDetailsFolder+"/").listFiles();
				Boolean isExisting = false;
				ArrayList<Integer> questionList= new ArrayList<Integer>();
				for (File file : files) {
					if (file.isFile() && file.getName().contains("xml")) {
						if(file.getName().equalsIgnoreCase(loggedInUser +".xml")){
							isExisting = true;
						}
					}
				}
				if(isExisting){
					jaxbContext = JAXBContext.newInstance(UserDetails.class);
					Unmarshaller userDetailsUn = jaxbContext.createUnmarshaller();
					userDetails = (UserDetails)userDetailsUn.unmarshal(userDetailFile);
					questionList = userDetails.getAnsweredQuestionList();
					Boolean isAttemptedQuestion = false;

					if(request.getParameter("timestamp") != null && !(request.getParameter("timestamp").equalsIgnoreCase("0"))){
						int questionId = Integer.parseInt(request.getParameter("timestamp"));
						for (Integer i: questionList){
							if(questionId == i){
								isAttemptedQuestion = true;
							}
						}
						if(!isAttemptedQuestion){
							questionList.add(questionId);
							userDetails.setAnsweredQuestionList(questionList);
							jaxbContext = JAXBContext.newInstance(UserDetails.class);
							Marshaller jaxbMarshallerUserDetail = jaxbContext.createMarshaller();				 
							jaxbMarshallerUserDetail.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
							jaxbMarshallerUserDetail.marshal(userDetails, System.out);
							jaxbMarshallerUserDetail.marshal(userDetails, userDetailFile);
						}

					}

				}
				else{
					jaxbContext = JAXBContext.newInstance(UserDetails.class);
					Marshaller jaxbMarshallerUserDetail = jaxbContext.createMarshaller();				 
					if(request.getParameter("timestamp") != null && !(request.getParameter("timestamp").equalsIgnoreCase("0"))){
						int i = Integer.parseInt(request.getParameter("timestamp"));
						questionList.add(i);
						userDetails.setAnsweredQuestionList(questionList);
					}
					jaxbMarshallerUserDetail.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
					jaxbMarshallerUserDetail.marshal(userDetails, System.out);
					jaxbMarshallerUserDetail.marshal(userDetails, userDetailFile);
				}
				model.addAttribute("answerresult",answerresult);
				model.addAttribute("index", userForm);				
			} 
			catch (JAXBException e) {
				e.printStackTrace();
			} 
		}

		return "showanswerresult";
	}
}